﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PSAdmin.Core.Domain;
using PSAdmin.Data.Repositories;
using PSAdmin.Data;

namespace PSAdmin.Tests
{
    /// <summary>
    /// Summary description for RepositoryTests
    /// </summary>
    [TestClass]
    public class RepositoryTests
    {
        public RepositoryTests()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        [DeploymentItem("hibernate.cfg.xml")]
        public void Can_create_script_that_needs_impersonation()
        {
            var repo = new PSScriptRepository(NHibernateHelper.OpenSession());

            var script = new PSScript
            {
                Name = "PC1000 file list",
                Description = @"Lists the contents of pc1000 c:\",
                CommandText = @"ls \\pc1000\c$"
            };

            repo.Save(script);

        }

        [TestMethod]
        [DeploymentItem("hibernate.cfg.xml")]
        public void Can_create_scripts()
        {
            var repo = new PSScriptRepository(NHibernateHelper.OpenSession());

            for (int i = 0; i < 25; i++)
            {
                var newscript = new PSScript
                {
                    Name = "Test script " + i,
                    Description = "This is a test script",
                    CommandText = "ping localhost"
                };
                repo.Save(newscript);
            }
        }

        [TestMethod]
        [DeploymentItem("hibernate.cfg.xml")]
        public void Can_create_script_with_variables()
        {
            var newscript = new PSScript {
                Name = "Ping script",
                Description = "This is a test script with variables",
                CommandText = "ping $hostname"
            };

            var variable = new PSScriptVariable
            {
                Name = "Host name",
                Description = "The host name you want to ping",
                Token = "hostname",
            };

            newscript.AddScriptVariable(variable);

            var repo = new PSScriptRepository(NHibernateHelper.OpenSession());
            repo.Save(newscript);
        }

        [TestMethod]
        [DeploymentItem("hibernate.cfg.xml")]
        public void Can_get_existing_script()
        {
            var repo = new PSScriptRepository(NHibernateHelper.OpenSession());
            var script = repo.GetById(65536);

            Assert.IsNotNull(script);
            Assert.AreEqual("Ping script", script.Name);
        }

        [TestMethod]
        [DeploymentItem("hibernate.cfg.xml")]
        public void Can_add_variable_to_existing_script()
        {
            var repo = new PSScriptRepository(NHibernateHelper.OpenSession());
            var script = repo.GetById(65536);

            var variable = new PSScriptVariable
            {
                Name = "Host name",
                Description = "The host name you want to ping",
                Token = "Hostname",
            };

            script.AddScriptVariable(variable);

            repo.Save(script);
        }

        [TestMethod]
        [DeploymentItem("hibernate.cfg.xml")]
        public void Can_delete_variable_from_script()
        {
            var repo = new PSScriptRepository(NHibernateHelper.OpenSession());

            var script = repo.GetById(163841);

            Assert.IsNotNull(script);
            Assert.AreEqual(0, script.Variables.Count());

            var scriptVariable = new PSScriptVariable {
                Name = "blah",
                Description = "blah blah",
                Token = "blah"
            };

            script.AddScriptVariable(scriptVariable);

            repo.Save(script);

            Assert.IsTrue(script.Variables.Count() > 0);

            script.Variables.Clear();

            repo.Save(script);           
        }



    }
}
